<?php
namespace app\mobile\controller;

use think\Controller;
use app\mobile\model\User;
use app\Common;
use think\Paginator;


class Admin extends Base
{
    /**
     * .主页
     * [index description]
     * @return [type] [description]
     */
	public function index(){
        
        $login = $_SESSION['Msg'];
        $time = date('Y-m-d', time());
        // 接入总量
        $getinall = User::table("wolive_chats")->distinct(true)->field('visiter_id')->where('business_id',$login['business_id'])->count();
        // 获取总会话量
        $chatall = User::table("wolive_chats")->where('business_id',$login['business_id'])->count();
        // 正在排队人数
        $waiter = User::table("wolive_queue")->where(['business_id'=>$login['business_id'],'state'=>'normal'])->where("service_id",0)->count();
        // 正在咨询的人
        $talking =User::table('wolive_queue')->where(['business_id'=>$login['business_id']])->where('state','normal')->where("service_id",'<>',0)->count();
        // 在线客服人数
        $services = User::table("wolive_service")->where(['business_id'=>$login['business_id'],'state'=>'online'])->count();
        // 今日会话量
        $nowchats = User::table("wolive_chats")->where('business_id',$login['business_id'])->where('timestamp','like',"{$time}%")->count();
        // 今日留言量
        $message = User::table('wolive_message')->where('business_id',$login['business_id'])->where('timestamp','like',"{$time}%")->count();
        // 留言总量
        $messageall = User::table('wolive_message')->where('business_id',$login['business_id'])->count();
        
        $data=[
          'getinall'=>$getinall,
          'chatall' =>$chatall,
          'waits' =>$waiter,
          'talks' =>$talking,
          'services'=>$services,
          'nowchats'=>$nowchats,
          'nowmsg'=>$message,
          'msgall'=>$messageall 
        ];

        $this->assign($data);

		return $this->fetch();
	}

    /**
     * .对话页面
     * [chat description]
     * @return [type] [description]
     */
	public function chat()
	{
      
      return $this->fetch();
	}

  /**
   * 
   * [talk description]
   * @return [type] [description]
   */
	public function talk()
	{ 

	  $login =$_SESSION['Msg'];
      $get =$this->request->get();
      $channel=htmlspecialchars($get['channel']);
      $avatar =htmlspecialchars($get['avatar']);
      $data =User::table('wolive_visiter')->where("channel",$channel)->find();
      
      $business =User::table('wolive_business')->where('business_id',$login['business_id'])->find();
      
      $this->assign("atype",$business['audio_state']);
      $this->assign("data",$data);
      $this->assign("avatar",$avatar);
      $this->assign('se',$login);
      $this->assign("img",$login['avatar']);	
	  return $this->fetch();
	}
  
  /**
   * .留言页面
   * [message description]
   * @return [type] [description]
   */
  public function message()
  { 

     $login = $_SESSION['Msg'];
     $get = $this->request->get();
     $userAdmin = User::table('wolive_message');
     $pageParam = ['query' => []];
     unset($get['page']);
     if ($get) {
            $pushtime = $get['pushtime'];

            if ($pushtime) {
                if ($pushtime == 1) {
                    $timetoday = date("Y-m-d", time());
                    $userAdmin->where('timestamp', 'like', $timetoday . "%");
                    $this->assign('pushtime', $pushtime);
                    $pageParam['query']['timestamp'] = $pushtime;
                } elseif ($pushtime == 7) {
                    $timechou = strtotime("-1 week");
                    $times = date("Y-m-d", $timechou);
                    $userAdmin->where('timestamp', ">", $times);
                    $this->assign('pushtime', $pushtime);
                    $pageParam['query']['timestamp'] = $pushtime;
                }
            }
        }

    $data = $userAdmin->where('business_id', $login['business_id'])->paginate(3, false, $pageParam);
    if($data->count() == 0){
    
       $this->assign('content','暂时没有留言数据');
    }else{
       $this->assign('content',"");
    }
    $page = $data->render();
 
    $this->assign('page', $page);
    $this->assign('msgdata', $data);
    return $this->fetch();
  }

  /**
   * 
   * [show description]
   * @return [type] [description]
   */
  public function show()
  { 
   
    $post =$this->request->post();

    $res =User::table('wolive_message')->where('mid', $post['mid'])->find();

    $data =['code'=>0,'data'=>$res];

    return $data;

  }

}